<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Search history' );

$turn = getCurrentTurn();

if( !isset( $_GET[ 'filter' ] ) ) {
	$filter = '';
}
else {
	$filter = formatInput( $_GET[ 'filter' ] );
}

if( !isset( $_GET[ 'sort' ] ) || $_GET[ 'sort' ] == '' ) {	
	$sort = 'date-desc';
}
else {
	$sort = formatInput( $_GET[ 'sort' ] );
}
$sort_selected = Array( 'rating-asc' => '', 'rating-desc' => '', 'relevance-asc' => '', 'relevance-desc' => '', 'date-asc' => '', 'date-desc' => '' );
if( $sort == 'rating-asc' ) {
	$order = 'rating';
	$asc = true;
}
elseif( $sort == 'rating-desc' ) {
	$order = 'rating';
	$asc = false;
}
elseif( $sort == 'relevance-asc' ) {
	$order = 'relevance';
	$asc = true;
}
elseif( $sort == 'relevance-desc' ) {
	$order = 'relevance';
	$asc = false;
}
elseif( $sort == 'date-asc' ) {
	$order = 'date';
	$asc = true;
}
else {
	$order = 'date';
	$asc = false;
}
$sort_selected[$sort] = "selected=\"selected\"";

if( !isset( $_GET[ 'type' ] ) || $_GET[ 'type' ] == '' ) {
	$type = 1;
}
else {
	$type = formatInput( $_GET[ 'type' ] );
}
$type_selected = Array( 0 => '', 1 => '', -1 => '');
$type_selected[$type] = "selected=\"selected\"";

if( !isset( $_GET[ 'page' ] ) || $_GET[ 'page' ] == '' ) {
	$page = 1;
}
else {
	$page = formatInput( $_GET[ 'page' ] );
}

print_history_tab( 'Search' );

?>
					<div class="blok">
						<h2><?php transprint( 'Search history' ); ?></h2><br />
						<br />
						<form action="search.php" method="get">
							<?php transprint( 'Filter on' ); ?> <input type="text" name="filter" size="15" value="<?php print( $filter ); ?>" /><br />
							<?php transprint( 'Type of history items' ); ?> <select name="type">
								<option value="1" <?php print( $type_selected[1] ); ?>><?php transprint( 'Written' ); ?></option>
								<option value="-1" <?php print( $type_selected[-1] ); ?>><?php transprint( 'Generated' ); ?></option>
								<option value="0" <?php print( $type_selected[0] ); ?>><?php transprint( 'All' ); ?></option>
							</select><br />
							<?php transprint( 'Order by' ); ?> <select name="sort">
								<option value="rating-desc" <?php print( $sort_selected['rating-desc'] ); ?>><?php transprint( 'rating, descending' ); ?></option>
								<option value="rating-asc" <?php print( $sort_selected['rating-asc'] ); ?>><?php transprint( 'rating, ascending' ); ?></option>
								<option value="relevance-desc" <?php print( $sort_selected['relevance-desc'] ); ?>><?php transprint( 'relevance, descending' ); ?></option>
								<option value="relevance-asc" <?php print( $sort_selected['relevance-asc'] ); ?>><?php transprint( 'relevance, ascending' ); ?></option>
								<option value="date-desc" <?php print( $sort_selected['date-desc'] ); ?>><?php transprint( 'date, descending' ); ?></option>
								<option value="date-asc" <?php print( $sort_selected['date-asc'] ); ?>><?php transprint( 'date, ascending' ); ?></option>
							</select><br />
							<input type="submit" value="<?php transprint( 'submit' ); ?>" />
						</form>
					</div>
					<br />
					<div class="blok">
<?php
	$total = count_search_history( $filter, $type );
	$items = search_history( $filter, $type, $order, $asc, $page );
	//print_r( $items );
	
	if( count( $items ) > 0 ) {
		
		if( $page * 10 > $total ) {
			$second = $total;
		}
		else {
			$second = $page * 10;
		} 
		
		$counter = 0;
		
		print( "<strong>" . translate( "Results" ) . " " . number_format( 1 + ( $page - 1 ) * 10 ) . " - " . number_format( $second ) . " out of " . number_format( $total ) . "</strong><br /><br />" );
		foreach( $items AS $item ) {
		    if( $item->ruler_id != -1 && $item->anonymous == 0 ) {
			    $query2 = "SELECT name " .
			    		"FROM ruler " .
			    		"WHERE ruler_id = " . $item->ruler_id;
			    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$author = "<a title=\"author stats\" href=\"../overview/ruler.php?name=" . $row2[ 'name' ] . "\">" . $row2[ 'name' ] . "</a>";
		    }
		    else {
		    	$author = translate( "World News Reporter" );
		    }
		    
		    if( $counter % 2 == 0 ) {
			    print( "						<div class=\"search-result even\">\r\n" );
		    }
		    else {
			    print( "						<div class=\"search-result\">\r\n" );
		    }
		    print( "<a href=\"history.php?id=" . $item->history_id . "\"><strong>" . $item->title . "</strong></a><br />" );
		    
		    if( $item->ruler_id != -1 ) {
			    $query2 = "SELECT SUM(history_rating.score) " .
			    		"FROM history_rating " .
			    		"JOIN history_tags ON history_tags.tag_id = history_rating.tag_id " .
			    		"WHERE history_id = $item->history_id AND positive >= negative";
			    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$total_score = $row2[ 'SUM(history_rating.score)' ];
			    $query2 = "SELECT SUM(history_rating.score) " .
			    		"FROM history_rating " .
			    		"JOIN history_tags ON history_tags.tag_id = history_rating.tag_id " .
			    		"WHERE history_id = $item->history_id AND positive < negative";
			    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
				$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
				$total_score -= $row2[ 'SUM(history_rating.score)' ];
				
				print( translate( "Rating" ) . ": " . number_format( round( $total_score ) ) );
			    $query2 = "SELECT tag " .
			    		"FROM history_tags " .
			    		"JOIN history_rating ON history_rating.tag_id = history_tags.tag_id " .
			    		"WHERE history_id = $item->history_id " .
			    		"ORDER BY history_rating.score DESC LIMIT 3";
			    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
			    $num = mysql_numrows( $result2 );
				if( $num > 0 ) {
					print( " (" );
					$i = 0;
					
					while ( $i < $num ) {
						if( $i > 0 ) {
							print( ", " );
						}
						print( mysql_result( $result2, $i, "tag" ) );
						$i ++;
					}
					print( ")" );
				}
				print( "<br />" );
		    }
		    
		    $total_comments = get_comment_count( $item->history_id );
			if( $total_comments == 0 ) {
				$text = translate( "No comments on this item" );
			}
			elseif( $total_comments == 1 ) {
				$text = translate( "There is one comment on this item" );
			}
			else {
				$text = translate( "There are" ) . " " . number_format( $total_comments ) . " " . translate( "comments on this item" );
			}
?>
							<small><?php transprint( 'Date' ); ?>: <?php print( $item->date ); ?></small><br />
							<small><?php transprint( 'Author' ); ?>: <?php print( $author ); ?></small><br />
							<?php print( $text ); ?>
						</div>
						<br />
						
<?php
			$counter ++;
		}
		if( $page > 1 ) {
			print( "<div class=\"text-left\"><a href=\"search.php?filter=$filter&type=$type&sort=$sort&page=" . ( $page - 1 ) . "\">" . translate( 'Previous' ). "</a></div>" );
		}
		if( $page * 10 < $total ) {
			print( "<div class=\"text-right\"><a href=\"search.php?filter=$filter&type=$type&sort=$sort&page=" . ( $page + 1 ) . "\">" . translate( 'Next' ) . "</a></div>" );
		}
		print( "<br />" );
	}
	else {
		print( translate( "Nothing found" ) );
	}
?>
					</div>
					<script type="text/javascript" src="history.js"></script>
<?php
aos_footer();
?>
